﻿using System;

namespace Work4
{
    //屏幕显示气象信息
    class ScreenDisplay
    {
        public void Display(object sender, EventArgs e)
        {
            MeteorologicalInformation mi = (MeteorologicalInformation)e;
            Console.WriteLine($"最新气象时间{mi.Time}，温度：{mi.Temperature}，湿度：{mi.Humidity}");
        }
    }
    //数据库保存气象信息
    class DatabaseSaving
    {
        public void Save(object sender, EventArgs e)
        {
            MeteorologicalInformation mi = (MeteorologicalInformation)e;
            Console.WriteLine($"数据库中保存了最新气象时间{mi.Time}的信息！");
        }
    }
    //定义类气象信息，MeteorologicalInformation
    //包含时间、温度、湿度等信息
    public class MeteorologicalInformation : EventArgs
    {
        public DateTime Time { get; set; }
        public int Temperature { get; set; }
        public int Humidity { get; set; }
    }
    //气象站类
    class MeteorologicalStation
    {
        //事件获取到新的气象信息
        public event EventHandler<MeteorologicalInformation> LatestMeteorologicalInformation;
        //属性气象信息
        public MeteorologicalInformation CurrentMeteorologicalInformation { get; set; }
        public MeteorologicalStation()
        {
            CurrentMeteorologicalInformation = new MeteorologicalInformation();
            CurrentMeteorologicalInformation.Time = DateTime.Now;
            CurrentMeteorologicalInformation.Temperature = 0;
            CurrentMeteorologicalInformation.Humidity = 0;
        }
        //获取新的气象信息
        public void AutomaticallyUpdateMeteorologicalInformation(int temperature, int humidity)
        {
            CurrentMeteorologicalInformation.Time = DateTime.Now;
            CurrentMeteorologicalInformation.Temperature = temperature;
            CurrentMeteorologicalInformation.Humidity = humidity;
            //调用触发事件
            DisplaysLatestMeteorologicalInformation();
        }
        //触发事件
        public void DisplaysLatestMeteorologicalInformation()
        {
            if (LatestMeteorologicalInformation != null)
            {
                LatestMeteorologicalInformation.Invoke(this, CurrentMeteorologicalInformation);
            }
        }
    }
    class Program
    {
        //定时器(1s)
        public static System.Timers.Timer timer = new System.Timers.Timer(1000);
        //气象站
        public static MeteorologicalStation ms = new MeteorologicalStation();
        //屏幕显示气象信息
        public static ScreenDisplay screen = new ScreenDisplay();
        //数据库保存气象信息
        public static DatabaseSaving db = new DatabaseSaving();
        public static void Main(string[] args)
        {
            //订阅事件
            ms.LatestMeteorologicalInformation += screen.Display;
            ms.LatestMeteorologicalInformation += db.Save;
            //设置执行一次（false）还是一直执行(true)
            timer.AutoReset = true;
            //设置是否执行System.Timers.Timer.Elapsed事件
            timer.Enabled = true;
            //绑定Elapsed事件
            timer.Elapsed += OnTimerElapsedHander;
            //一直执行Elapsed事件(1s执行一次)
            timer.Start();
            Console.ReadLine();
        }
        public static void OnTimerElapsedHander(object sender, EventArgs eventArgs)
        {
            ms.AutomaticallyUpdateMeteorologicalInformation(13, 17);
        }
    }
}